import { Points,BallTagType, BallType, Line_H, Line_W, StickLength } from "../../Script/Game2/Config";
import EventManager from "../../Script/framework/core/EventManager";
import Tools from "../../Script/Game2/Tools";
 

/**
 * 球
 */
const {ccclass, property} = cc._decorator;

@ccclass
export default class Ball extends cc.Component {

    @property(cc.Label)
    tip:cc.Label =null;
    spCom:any = null;
    distance:number = Line_H;
    multiplierX:number = 0;
    multiplierY:number = 0;
    isClicked:boolean = false;
    isContact:boolean = false;
    config:Points = null;
    pcc:cc.PhysicsCircleCollider = null;
    rb:cc.RigidBody = null;
    n:any = null;
    nameV:string = ""
    onLoad(){  
        this.spCom = this.node.getComponent("SpriteEx"); 
        this.node.removeComponent(cc.RopeJoint);
    }

    unuse(){
        this.unscheduleAllCallbacks();
        this.node.stopAllActions();
        this.config = null;
        this.multiplierX = 0;
        this.multiplierY= 0;
        this.isClicked = false;
        this.isContact = false;
        this.node.position = cc.v3(0.0);
    }
 
    setTip(str){
        this.tip.string = str;
        this.nameV = str;
    }

    start(){  
    }
 
    init(config:Points,seq:number){  
        this.config = config;
        this.isClicked = false;
        this.isContact = false;
        this.multiplierX = Math.sqrt(StickLength * StickLength - StickLength / 2 * StickLength / 2),
        this.multiplierY = StickLength,  
        this.node.x = Math.floor(config.x * this.multiplierX);
        this.node.y =  - Math.floor(config.y * this.multiplierY); 
        this.node.zIndex = this.node.y ;
        this.pcc =  this.node.getComponent(cc.PhysicsCircleCollider)
        let spCom = this.node.getComponent("SpriteEx");
        this.rb =  this.node.getComponent(cc.RigidBody)  
        this.rb.awake = true;
        this.rb.type = cc.RigidBodyType.Static;
        this.pcc.tag =  BallTagType.WHITE
        spCom.index = BallType.WHITE; 
        // this.rb.gravityScale = 1; 
        // this.pcc.density = 1;
        // this.node.getComponent(cc.PhysicsCircleCollider).apply(); 
        //是固定的
        if(config.fixed){
            this.rb.type = cc.RigidBodyType.Static;
        } 
        //可以点击
        if(config.clickable){
            this.node.on(cc.Node.EventType.TOUCH_END,this.clickHandle,this);
            spCom.index = BallType.FIXED;
            this.pcc.tag =  BallTagType.CLICK
        }

        //不是固定的并且不可以点击
        if(config.clickable == false && config.fixed == false){
            spCom.index = BallType.BLACK; 
            this.pcc.tag =  BallTagType.BLACK 
            this.rb.enabledContactListener = false; 
        } 
         //不是固定的并且不可以点击 
        else if(config.clickable == false && config.fixed){
            spCom.index = BallType.WHITE; 
            this.pcc.tag =  BallTagType.WHITE
            this.rb.type = cc.RigidBodyType.Static;
        }
    }
 
    opendDynamic(){
        let config = this.config;
        if(config.clickable == false && config.fixed == false){ 
            this.rb.type = cc.RigidBodyType.Dynamic; 
        } 
    }
 
    onBeginContact(contact,selfCollider,otherCollider){
        if(this.isContact)return;
        if((otherCollider.tag == BallTagType.WHITE && selfCollider.tag == BallTagType.BLACK)  || (otherCollider.tag == BallType.BLACK && selfCollider.tag == BallType.WHITE)){
            this.isContact = true;
            this.spCom.index = BallType.BLACK;
            EventManager.emit("playShatter"); 
            this.pcc.tag =  BallTagType.BLACK  
            this.rb.enabledContactListener = false;  
            this.scheduleOnce(()=>{  
                this.rb.type = cc.RigidBodyType.Dynamic;
                this.setZore();
                EventManager.emit("checkPassLevel"); 
            },0.1)
        } 
        //砸到可以点击的了
        else if((otherCollider.tag == BallTagType.CLICK && selfCollider.tag == BallTagType.BLACK)  || (otherCollider.tag == BallTagType.BLACK && selfCollider.tag == BallTagType.CLICK)){
           
            this.clickHandle()
            EventManager.emit("playShatter");
           
        }
    }

    setZore(){ 
        let rjs:cc.RopeJoint[] =   this.node.getComponents(cc.RopeJoint)
        if(rjs.length >= 2){ 
            this.pcc.density = 1;   
            this.node.getComponent(cc.RigidBody).gravityScale = 1
            this.node.getComponent(cc.PhysicsCircleCollider).apply(); 
        } 

        this.pcc.density = 1;   
        this.node.getComponent(cc.RigidBody).gravityScale = 0.5
        this.node.getComponent(cc.PhysicsCircleCollider).apply(); 
    }
 
    clickHandle(){
        this.node.off(cc.Node.EventType.TOUCH_END,this.clickHandle);
        if(this.isClicked)return;
        Tools.playEffect("jiedian/sound/click",false); 
        this.isClicked = true;
        this.node.zIndex = 999; 
        this.spCom.index = BallType.BLACK;
        this.scheduleOnce(()=>{  
            this.rb.type = cc.RigidBodyType.Dynamic;
            this.setZore();
            EventManager.emit("checkPassLevel");
        },0.1) 
        this.rb.enabledContactListener = true;
        this.pcc.tag =  BallTagType.BLACK
        EventManager.emit("startDynamic");
        EventManager.emit("clickCheck"); 
        EventManager.emit("playSqueek") 
    }   
}
